package com.xyht.sca_s.student_manage_system.modules.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.inform.entity.SmsInform;
import com.xyht.sca_s.student_manage_system.modules.inform.entity.vo.SmsExamineUserInfoVo;
import com.xyht.sca_s.student_manage_system.modules.inform.mapper.SmsInformMapper;
import com.xyht.sca_s.student_manage_system.modules.news.constant.CollectStatusConstant;
import com.xyht.sca_s.student_manage_system.modules.news.entity.SmsNews;
import com.xyht.sca_s.student_manage_system.modules.news.entity.SmsNewsInformCollect;
import com.xyht.sca_s.student_manage_system.modules.news.entity.vo.SmsUserCollectVo;
import com.xyht.sca_s.student_manage_system.modules.news.mapper.SmsNewsInformCollectMapper;
import com.xyht.sca_s.student_manage_system.modules.news.mapper.SmsNewsMapper;
import com.xyht.sca_s.student_manage_system.modules.news.service.SmsNewsInformCollectService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 收藏表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-05-08
 */
@Service
public class SmsNewsInformCollectServiceImpl extends ServiceImpl<SmsNewsInformCollectMapper, SmsNewsInformCollect> implements SmsNewsInformCollectService {

    @Autowired
    private SmsNewsInformCollectMapper smsNewsInformCollectMapper;
    @Autowired
    private SmsNewsMapper smsNewsMapper;
    @Autowired
    private SmsInformMapper smsInformMapper;
    @Autowired
    private CacheUtil cacheUtil;

    @Override
    public ResponseResult getCollectList(String userId,Integer pageNum, Integer pageSize) {
        if(isNullOrEmpty(userId)){
            return CommonResult.failed(CommonCodeEnum.UNAUTHORIZED);
        }
        QueryWrapper<SmsNewsInformCollect> userCollectQueryWrapper = new QueryWrapper<>();
        Page<SmsNewsInformCollect> page = new Page<>(pageNum,pageSize);
        List<SmsUserCollectVo> smsUserCollectVoList = new ArrayList<>();
        userCollectQueryWrapper.lambda()
                .eq(SmsNewsInformCollect::getCollectUserId,userId)
                .eq(SmsNewsInformCollect::getCollectStatus,CollectStatusConstant.COLLECT_STATUS_COLLECT)
                .orderByDesc(SmsNewsInformCollect::getCreateTime);
        smsNewsInformCollectMapper.selectPage(page,userCollectQueryWrapper);
        List<SmsNewsInformCollect> smsNewsInformCollects = smsNewsInformCollectMapper.selectList(userCollectQueryWrapper);
        if(smsNewsInformCollects != null && smsNewsInformCollects.size() > 0){
            for (SmsNewsInformCollect smsNewsInformCollect : smsNewsInformCollects) {
                SmsUserCollectVo smsUserCollectVo = new SmsUserCollectVo();
                //0 为新闻
                if(smsNewsInformCollect.getSign() == 0){
                    SmsNews smsNews = smsNewsMapper.selectById(smsNewsInformCollect.getCollectInfoId());
                    smsUserCollectVo.setId(smsNews.getId());
                    smsUserCollectVo.setTopic(smsNews.getNewsTopic());
                    smsUserCollectVo.setEventType(smsNews.getEventType());
                    smsUserCollectVo.setCover(smsNews.getCover());
                    smsUserCollectVo.setNewsOrInformType(smsNews.getNewsType());
                    smsUserCollectVo.setStartTime(smsNews.getStartTime());
                    smsUserCollectVo.setEndTime(smsNews.getEndTime());
                    smsUserCollectVo.setIsRead(smsNews.getIsRead());
                    SmsUser userInfo = cacheUtil.getUserInfo(smsNews.getNewsId());
                    if(userInfo != null){
                        SmsExamineUserInfoVo smsExamineUserVo = new SmsExamineUserInfoVo();
                        BeanUtils.copyProperties(userInfo,smsExamineUserVo);
                        smsUserCollectVo.setSmsExamineUserVo(smsExamineUserVo);
                    }
                    smsUserCollectVo.setType("0");

                }
                //1 为通知
                if(smsNewsInformCollect.getSign() == 1){
                    SmsInform smsInform = smsInformMapper.selectById(smsNewsInformCollect.getCollectInfoId());
                    smsUserCollectVo.setId(smsInform.getId());
                    smsUserCollectVo.setTopic(smsInform.getEventTopic());
                    smsUserCollectVo.setEventType(smsInform.getEventType());
                    smsUserCollectVo.setCover(smsInform.getCover());
                    smsUserCollectVo.setNewsOrInformType(smsInform.getNotificationType());
                    smsUserCollectVo.setStartTime(smsInform.getStartTime());
                    smsUserCollectVo.setEndTime(smsInform.getEndTime());
                    smsUserCollectVo.setIsRead(smsInform.getIsRead());
                    SmsUser userInfo = cacheUtil.getUserInfo(smsInform.getIssuerId());
                    if(userInfo != null){
                        SmsExamineUserInfoVo smsExamineUserVo = new SmsExamineUserInfoVo();
                        BeanUtils.copyProperties(userInfo,smsExamineUserVo);
                        smsUserCollectVo.setSmsExamineUserVo(smsExamineUserVo);
                    }
                    smsUserCollectVo.setType("1");
                }
                smsUserCollectVoList.add(smsUserCollectVo);
            }
        }
        int total = (int) page.getTotal();
        return CommonResult.success(smsUserCollectVoList,total);
        }
}
